package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.system.domain.ZtfVideoTable;
import com.ruoyi.system.mapper.ZtfVideoTableMapper;
import com.ruoyi.system.service.IZtfVideoTableService;
import com.ruoyi.system.utils.SysTencentCOSUtil;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-30
 */
@Service
public class ZtfVideoTableServiceImpl implements IZtfVideoTableService
{
    @Autowired
    private ZtfVideoTableMapper ztfVideoTableMapper;

    @Autowired
    private SysTencentCOSUtil sysTencentCOSUtil;
    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public ZtfVideoTable selectZtfVideoTableById(String id)
    {
        return ztfVideoTableMapper.selectZtfVideoTableById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param ztfVideoTable 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<ZtfVideoTable> selectZtfVideoTableList(ZtfVideoTable ztfVideoTable)
    {
        //获取视频列表
      List<ZtfVideoTable> ztfVideoTableList=ztfVideoTableMapper.selectZtfVideoTableList(ztfVideoTable);
        //用stream流求和
        Integer totalOver=ztfVideoTableList.stream().mapToInt(ZtfVideoTable::getIsOver).sum();
        for (ZtfVideoTable videoTable : ztfVideoTableList) {
            //已学习多少节
            videoTable.setZtfStudyClasshortB(totalOver);
        }
        return ztfVideoTableList;
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param ztfVideoTable 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertZtfVideoTable(ZtfVideoTable ztfVideoTable)
    {
        ztfVideoTable.setCreateTime(new Date());
        ztfVideoTable.setUploadTime(new Date());
        ztfVideoTable.setUpdateTime(new Date());
        ztfVideoTable.setVideoUrl(ztfVideoTable.getFileUrl());
        return ztfVideoTableMapper.insertZtfVideoTable(ztfVideoTable);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param ztfVideoTable 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateZtfVideoTable(ZtfVideoTable ztfVideoTable)
    {
        return ztfVideoTableMapper.updateZtfVideoTable(ztfVideoTable);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteZtfVideoTableByIds(String[] ids)
    {
        //根据id查询出视频
        for (String id : ids) {
            //根据传递的id查询出视频
            ZtfVideoTable ztfVideoTable=ztfVideoTableMapper.selectZtfVideoTableById(id);
           //获取视频路径
            String videoUrl= ztfVideoTable.getVideoUrl();
            //https://lhcos-c88d2-1358334170.cos.ap-beijing.myqcloud.com/images/e32c6c8f-aa94-40b0-8604-b4a467f626b9.mp4
            int start = videoUrl.indexOf("://") + 3;
            int end = videoUrl.indexOf(".cos.ap-beijing.myqcloud.com");
            //截取出bucketName
            String bucketName = videoUrl.substring(start, end);
            //截取出key
            String key=  videoUrl.substring(videoUrl.indexOf(".com") + 4);
            //删除服务器的指定文件
            sysTencentCOSUtil.deleteFile(bucketName,key);
        }
        return ztfVideoTableMapper.deleteZtfVideoTableByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteZtfVideoTableById(String id)
    {
        return ztfVideoTableMapper.deleteZtfVideoTableById(id);
    }
}
